//
//  RegisterViewController.m
//  Kokozu
//
//  Created by Bismarrck on 11-7-17.
//  Copyright 2011年 Tsinghua University. All rights reserved.
//

#import "RegisterViewController.h"
#import "UserDefault.h"
#import "UIViewController+BarItem.h"

@interface RegisterViewController ()

- (SEL)actionForKey:(NSString *)actionKey;
- (void)getPin;
- (void)regMyInfo;

@end

@implementation RegisterViewController

@synthesize phoneNumberField;
@synthesize passwordField;
@synthesize passCheckField;
@synthesize pinField;
@synthesize button;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"注册帐户";
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background_light.png"]];
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc
{
    [phoneNumberField release];
    [passwordField release];
    [passCheckField release];
    [pinField release];
    [button release];
    
    [super dealloc];
}

#pragma mark - View lifecycle

- (void)dismissModalViewController
{
    [self dismissModalViewControllerAnimated:YES];
}

- (void)pop
{
    NSLog(@"pop");
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)endEditing {
    [self resignFirstResponder];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.pinField.delegate = self;
    [passwordField addTarget:self action:@selector(endEditing) forControlEvents:UIControlEventEditingDidEndOnExit];
    [passCheckField addTarget:self action:@selector(endEditing) forControlEvents:UIControlEventEditingDidEndOnExit];
    [pinField addTarget:self action:@selector(endEditing) forControlEvents:UIControlEventEditingDidEndOnExit];
    [phoneNumberField addTarget:self action:@selector(endEditing) forControlEvents:UIControlEventEditingDidEndOnExit];
    
    // Set the cancel button
    [self createBarItemOn:kNavBarItemSideLeft frame:kNavBarLeftItemFrame title:@"返回" target:self action:@selector(pop)];
    
    // Set the title
    [self addTitleToNavigationBar:@"注册"];
}

#pragma mark -
#pragma mark UITextField Delegate

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    [UIView beginAnimations:@"showKeyboardAnimation" context:nil];
    [UIView setAnimationDuration:0.30];
    self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y- 100, self.view.frame.size.width, self.view.frame.size.height);
    [UIView commitAnimations];
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.30];
    CGRect rect = self.view.frame;
    rect.origin.y += 100;
    self.view.frame = rect;
    [UIView commitAnimations];
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    
    self.button = nil;
    self.phoneNumberField = nil;
    self.pinField = nil;
    self.passwordField = nil;
    self.passCheckField = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -
#pragma mark - Interface Builder Method

- (IBAction)buttonPressed
{
    NSString *title = [[self.button titleLabel] text];
    [self performSelector:[self actionForKey:title]];
}

#pragma mark -
#pragma mark - Private Method

- (SEL)actionForKey:(NSString *)actionKey
{
    if ([actionKey isEqualToString:@"获取验证码" ]) {
        return @selector(getPin);
    }
    else {
        return @selector(regMyInfo);
    }
}

- (void)getPin
{
    // This is the test code
    if ([[passCheckField text] isEqualToString:[passwordField text]]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil 
                                                        message:@"PIN: 123456" 
                                                       delegate:nil cancelButtonTitle:@"收到" 
                                              otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
        
        [self.button setTitle:@"注册个人帐户" forState:UIControlStateNormal];
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil 
                                                        message:@"两次密码不一致，请重新输入" 
                                                       delegate:nil 
                                              cancelButtonTitle:@"重新输入" 
                                              otherButtonTitles:nil, nil];
        [alert show];
        
        [passwordField setText:@""];
        [passCheckField setText:@""];
        
        [alert release];
    }
}

- (void)regMyInfo
{
    // This is the test code
    NSString *result;
    
    if ([[pinField text] isEqualToString:@"123456"]) {
        NSString *user_name = [phoneNumberField text];
        NSString *user_pass = [passwordField text];
        
        [[NSUserDefaults standardUserDefaults] setValue:user_name forKey:@"user_name"];
        [[NSUserDefaults standardUserDefaults] setValue:user_pass forKey:@"user_pass"];
        
        result = @"注册成功";
        
        NSLog(@"%@", USER_DEFAULT_NAME);
    }
    else {
        result = @"注册失败, PIN码错误";
    }
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil 
                                                    message:result 
                                                   delegate:nil 
                                          cancelButtonTitle:@"明白" 
                                          otherButtonTitles:nil, nil];
    [alert show];
    [alert release];
    
    [self.button setTitle:@"获取验证码" forState:UIControlStateNormal];
}


@end
